package com.example.project_out_in;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import Business.bSQL_Heper;
import Entities.EOut_side_total;
import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;

import com.listview.ulti.MyStringPair;
import com.listview.ulti.MyStringPairAdapter;

public class Fragment_Resport extends Fragment{
	EditText mEdit;
	ImageButton img;
	View m;
	bSQL_Heper sqlheper;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		sqlheper = new bSQL_Heper(this.getActivity());
		final View root = inflater.inflate(R.layout.main2, container, false);
		img =(ImageButton)root.findViewById(R.id.imageButton1);
		mEdit = (EditText)root.findViewById(R.id.editText1);
		mEdit.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				Bindata();
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				//Log.d("beforeTextChanged", s.toString());

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				//Log.d("afterTextChanged", s.toString());
			}
		}); 

		img.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				selectDate(root);
			}
		});
		m=root;
		Bindata();
		return root;
	}

	

	void Bindata(){
		try{
    	Date day = null;
    	 //SimpleDateFormat sdf = new SimpleDateFormat("yyyy/mm/dd");
    	 String fDate = new SimpleDateFormat("yyyy/MM/dd").format(new Date());
         try {
        	 SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy"); 
        	 //Date dateObj = curFormater.parse(dateStr); 
        	 day = curFormater.parse(mEdit.getText().toString());
        	 
         }catch(Exception ex){
             ex.printStackTrace();
         }
         if(day!=null)
        	 fDate = new SimpleDateFormat("yyyy/MM/dd").format(day);
        
    	
     Log.d("loi", "fDate " + fDate +"  day: "+day);
    	
		List<EOut_side_total> list = sqlheper.getAllOutsite_Insite(fDate);
    	ListView view = (ListView)m.findViewById(R.id.list);
        final List<MyStringPair> myStringPairList = MyStringPair.makeData(list);
        MyStringPairAdapter adapter = new MyStringPairAdapter(this, myStringPairList);
              
        view.setAdapter(adapter);
		}catch(Exception ex){}
    }

	public void selectDate(View view) {
		DialogFragment newFragment = new SelectDateFragment();
		newFragment.show(this.getActivity().getSupportFragmentManager(), "DatePicker");
	}
	public void populateSetDate(int year, int month, int day) {

		mEdit.setText(day+"/"+month+"/"+year);

	}
	@SuppressLint("ValidFragment")
	public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
		@Override
		public Dialog onCreateDialog(Bundle savedInstanceState) {
			final Calendar calendar = Calendar.getInstance();
			int yy = calendar.get(Calendar.YEAR);
			int mm = calendar.get(Calendar.MONTH);
			int dd = calendar.get(Calendar.DAY_OF_MONTH);
			return new DatePickerDialog(getActivity(), this, yy, mm, dd);
		}

		public void onDateSet(DatePicker view, int yy, int mm, int dd) {
			populateSetDate(yy, mm+1, dd);
		}
	}
}
